import hashlib

import scrapy
from ..items import MyFileItem
from ..Functional.LanguageFunction import Languagefunction
import re


class mocgovSpider(scrapy.Spider):
    name = 'khm_mocgov'
    allowed_domains = ['moc.gov.kh/en-us']
    # 柬埔寨商务部网站地址
    start_urls = ['http://www.moc.gov.kh/en-us/official-documents/pdfpidx/0']
    id = 0
    Current_page = ''

    def parse(self, response):
        # 获得成员列表
        trs = response.xpath('//*[@id="dnn_ctr9682_ViewMenu_simplemenu"]/li')
        for tr in trs:
            # 法律分类
            sortFS = tr.xpath('./a//text()')
            sortF = ''
            if len(sortFS):
                sortF = sortFS.get().strip()
            # 分类网址
            webUrlS = tr.xpath('./a/@href')
            if len(webUrlS):
                webUrl = webUrlS.get().strip() + '/pdfpidx/0'
                yield response.follow(webUrl, callback=self.websited, dont_filter=True, meta={'sortF': sortF})

    def websited(self, response):
        nowurl = response.url
        sortF = response.meta['sortF']
        trs = response.xpath('//tr[@class="list-item"]')
        for tr in trs:
            # 法律名称
            legalNameS = tr.xpath('./td/a//text()')
            legalName = ''
            if len(legalNameS):
                for legalNamel in legalNameS:
                    legalName = legalName + ' ' + legalNamel.get().strip()
                legalName = re.sub(r'''^ *''', r'''''', legalName)  # 去除开头的空格
                legalName = re.sub(r'''"''', r"'", legalName)  # 将双引号变为单引号
            # 生效时间
            effectiveDateS = tr.xpath('./td/span/text()')
            effectiveDate = ''
            if len(effectiveDateS):
                effectiveDate = effectiveDateS.get().strip()
            # 详细网址
            detailUrlS = tr.xpath('./td/a/@href')
            if len(detailUrlS):
                detailUrla = tr.xpath('./td/a')[0]
                yield response.follow(detailUrla, callback=self.detailed, dont_filter=True,
                                      meta={'sortF': sortF, 'legalName': legalName, 'effectiveDate': effectiveDate})
        # 翻页
        page_nowS = response.xpath('//tr[@class="list-paging"]//table//td/span/text()')
        if len(page_nowS):
            Current_pagenow = page_nowS.get().strip()
            pagenow = int(Current_pagenow)
            pageS = response.xpath('//tr[@class="list-paging"]//table//td/a')
            for page in pageS:
                Current_page = page.xpath('./text()')
                if len(Current_page):
                    Current_page = Current_page.get().strip()
                    try:
                        nextpage = int(Current_page)
                        if nextpage == pagenow + 1:
                            nextUrl = re.sub(r'''[0-9]+$''', str(pagenow), nowurl)  # 网址编码比页码小1
                            yield response.follow(nextUrl, callback=self.websited, dont_filter=True,
                                                  meta={'sortF': sortF})
                            break
                    except Exception as e:
                        print(str(e))
        else:
            print("找不到对象")

    # 解析详情页
    def detailed(self, response):
        item = MyFileItem()
        if response.status == 200:
            def iadd():
                self.id += 1
                return self.id

            sortF = response.meta['sortF']
            legalName = response.meta['legalName']
            effectiveDate = response.meta['effectiveDate']
            sortB = Languagefunction().recongnizelanguage(legalName)

            # 法律详细网址
            nowurl = response.url
            detailUrl = nowurl
            download = re.findall(r'''\.pdf''', detailUrl)

            if len(download):
                # 下载文件名
                fina = 'f' + str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())
                # 唯一ID
                systemid = str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())
                iid = iadd()

                item['file_urls'] = ''
                item['country'] = 'Cambodia'
                item['website'] = 'mocgov'
                item['modular'] = 'law'
                item['title'] = ''
                item['ext'] = 'pdf'
                item['fina'] = fina
                item['chapNo'] = ''
                item['detailUrl'] = ''
                item['downloadUrl'] = detailUrl
                item['htmls'] = ''
                item['htmlUrl'] = ''
                item['abstract'] = ''
                item['abstractUrl'] = ''

                item['LegalName'] = legalName
                item['Organizaation'] = ''
                item['PublishDate'] = ''
                item['EffectiveDate'] = effectiveDate
                item['SortA'] = 'LAWCOUNTRYJPZ'
                item['SortB'] = sortB
                item['SortC'] = ''
                item['SortD'] = ''
                item['SORTE'] = ''
                item['SORTF'] = sortF
                item['Keyword'] = ''
                item['SORTG'] = ''
                item['ChapNo'] = ''
                item['Articles'] = ''
                item['Chapter'] = ''
                item['Section'] = ''
                item['SYS_FLD_DIGITFILENAME'] = fina
                item['FileUrl'] = ''
                item['DownLoadUrl'] = detailUrl
                item['DownLoadWebNameC'] = '柬埔寨商务部网站'
                item['DownLoadWebNameE'] = 'Ministry of Commerce Cambodia'
                item['SYSID'] = systemid
                item['Website'] = 'KINGDOM OF CAMBODIA, Ministry of Commerce'
                item['Isconversion'] = '0'
                item['Revisionmark'] = ''

                yield item







